findAllMarkets();

/**
 * 查询市场列表
 */
function findAllMarkets() {
    var data = {
        "opend": 1,
        "evented": 1,
        "eventId": baseFn.getQueryString('eventId')
    }
    $.ajax({
        url: baseUrl.base + '/order/getMarketList',
        type: 'get',
        data: data,
        success: function (data) {

            if (data.code == '0') {
                var markets = data.results;
                //比赛基本信息
                var eventInfo = data.results.event;

                $('#home_name_1').text(eventInfo.homeTeamName);
                $('#home_name_2').text(eventInfo.homeTeamName);

                $('#away_name_1').text(eventInfo.awayTeamName);
                $('#away_name_2').text(eventInfo.awayTeamName);

                var competitionName = eventInfo.competitionName;
                var teamName = eventInfo.homeTeamName + 'VS' + eventInfo.awayTeamName;
                var eventTime = eventInfo.eventTime / 10000;
                var score = eventInfo.homeTeamScore + ' : ' + eventInfo.awayTeamScore;
                var state = eventInfo.state;
                var stage = eventInfo.stage;

                var category = '';
                category += competitionName + "  ";
                category += teamName + ' ';

                var str = '';
                if (state == 3) {
                    if (stage == 2) {
                        str = "比赛正在上半场";
                    } else if (stage == 4) {
                        str = "比赛正在中场休息";
                    } else if (stage == 8) {
                        str = "比赛正在下半场";
                    } else if (stage == 16) {
                        str = "比赛全场结束";
                    } else if (stage == 32) {
                        str = "比赛常规时间结束";
                    } else if (stage == 64) {
                        str = "比赛正在加时赛上半场";
                    } else if (stage == 128) {
                        str = "比赛正在加时赛中场休息";
                    } else if (stage == 256) {
                        str = "比赛正在加时赛下半场";
                    } else if (stage == 512) {
                        str = "比赛加时赛结束";
                    } else if (stage == 1024) {
                        str = "比赛正在点球大战";
                    }
                }
                category += str;

                if (state != 0) {
                    category += '比分' + score;
                }

                $('#card-category').text(category);

                $('#card-title').text('开赛时间:' + timestampToTime(eventTime));

                //启用，禁用
                var disRuleTypes = eventInfo.disRuleTypes;
                if (null != disRuleTypes && disRuleTypes.indexOf("100") != -1) {
                    document.getElementById("enabled_01").setAttribute("class", "btn btn-danger btn-sm");
                    document.getElementById("enabled_01").setAttribute("status", "0");
                    $('#enabled_01').text('已禁用');
                } else {
                    document.getElementById("enabled_01").setAttribute("class", "btn btn-info btn-sm");
                    document.getElementById("enabled_01").setAttribute("status", "1");
                    $('#enabled_01').text('已启用');
                }
                if (null != disRuleTypes && disRuleTypes.indexOf('130') != -1) {
                    document.getElementById("enabled_02").setAttribute("status", "0");
                    document.getElementById("enabled_02").setAttribute("class", "btn btn-danger btn-sm");
                    $('#enabled_02').text('已禁用');
                } else {
                    document.getElementById("enabled_02").setAttribute("status", "1");
                    document.getElementById("enabled_02").setAttribute("class", "btn btn-info btn-sm");
                    $('#enabled_02').text('已启用');
                }
                if (null != disRuleTypes && disRuleTypes.indexOf('140') != -1) {
                    document.getElementById("enabled_05").setAttribute("status", "0");
                    document.getElementById("enabled_05").setAttribute("class", "btn btn-danger btn-sm");
                    $('#enabled_05').text('已禁用');
                } else {
                    document.getElementById("enabled_05").setAttribute("status", "1");
                    document.getElementById("enabled_05").setAttribute("class", "btn btn-info btn-sm");
                    $('#enabled_05').text('已启用');
                }
                if (null != disRuleTypes && disRuleTypes.indexOf('350') != -1) {
                    document.getElementById("enabled_03").setAttribute("status", "0");
                    document.getElementById("enabled_03").setAttribute("class", "btn btn-danger btn-sm");
                    $('#enabled_03').text('已禁用');
                } else {
                    document.getElementById("enabled_03").setAttribute("status", "1");
                    document.getElementById("enabled_03").setAttribute("class", "btn btn-info btn-sm");
                    $('#enabled_03').text('已启用');
                }
                if (null != disRuleTypes && disRuleTypes.indexOf('410') != -1) {
                    document.getElementById("enabled_04").setAttribute("status", "0");
                    document.getElementById("enabled_04").setAttribute("class", "btn btn-danger btn-sm");
                    $('#enabled_04').text('已禁用');
                } else {
                    document.getElementById("enabled_04").setAttribute("status", "1");
                    document.getElementById("enabled_04").setAttribute("class", "btn btn-info btn-sm");
                    $('#enabled_04').text('已启用');
                }

                var market = searchMarket(markets, 100);
                //胜平负
                if (null != market) {
                    var selection = searchSelection(market, 1);
                    if (null != selection) {
                        $('#match_0').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 0);
                    if (null != selection) {
                        $('#match_1').text(selection.backOdds);
                    }
                    selection = searchSelection(market, -1);
                    if (null != selection) {
                        $('#match_2').text(selection.backOdds);
                    }

                    $('#market_status_1').text(marketStatus(market));
                    document.getElementById("count_01").setAttribute("event_id", market.eventId);
                    document.getElementById("count_01").setAttribute("market_id", market.marketId);
                }

                //进球单双数
                market = searchMarket(markets, 130);
                if (null != market) {
                    var selection = searchSelection(market, 1);
                    if (null != selection) {
                        $('#even_0').text(selection.backOdds);
                    }
                    selection = searchSelection(market, -1);
                    if (null != selection) {
                        $('#even_1').text(selection.backOdds);
                    }
                    $('#market_status_2').text(marketStatus(market));
                }
                //下一个进球
                market = searchMarket(markets, 350);
                if (null != market) {
                    var selection = searchSelection(market, 1);
                    if (null != selection) {
                        $('#next_0').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 0);
                    if (null != selection) {
                        $('#next_1').text(selection.backOdds);
                    }
                    selection = searchSelection(market, -1);
                    if (null != selection) {
                        $('#next_2').text(selection.backOdds);
                    }
                    $('#market_status_3').text(marketStatus(market));
                }

                //进球总数
                market = searchMarket(markets, 410);
                if (null != market) {
                    var selection = searchSelection(market, 1);
                    if (null != selection) {
                        $('#goals_1').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 2);
                    if (null != selection) {
                        $('#goals_2').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 3);
                    if (null != selection) {
                        $('#goals_3').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 4);
                    if (null != selection) {
                        $('#goals_4').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 5);
                    if (null != selection) {
                        $('#goals_5').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 6);
                    if (null != selection) {
                        $('#goals_6').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 7);
                    if (null != selection) {
                        $('#goals_7').text(selection.backOdds);
                    }
                    selection = searchSelection(market, -1);
                    if (null != selection) {
                        $('#goals_8').text(selection.backOdds);
                    }
                    $('#market_status_4').text(marketStatus(market));
                }

                //正确比分
                market = searchMarket(markets, 140);
                if (null != market) {
                    var selection;
                    //n:0
                    selection = searchSelection(market, 10001);
                    if (null != selection) {
                        $('#score_0_0').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 10002);
                    if (null != selection) {
                        $('#score_1_0').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 10003);
                    if (null != selection) {
                        $('#score_2_0').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 10004);
                    if (null != selection) {
                        $('#score_3_0').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 10005);
                    if (null != selection) {
                        $('#score_4_0').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 10006);
                    if (null != selection) {
                        $('#score_5_0').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 10007);
                    if (null != selection) {
                        $('#score_6_0').text(selection.backOdds);
                    }
                    //n:1
                    selection = searchSelection(market, 20001);
                    if (null != selection) {
                        $('#score_0_1').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 20002);
                    if (null != selection) {
                        $('#score_1_1').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 20003);
                    if (null != selection) {
                        $('#score_2_1').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 20004);
                    if (null != selection) {
                        $('#score_3_1').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 20005);
                    if (null != selection) {
                        $('#score_4_1').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 20006);
                    if (null != selection) {
                        $('#score_5_1').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 20007);
                    if (null != selection) {
                        $('#score_6_1').text(selection.backOdds);
                    }
                    //n:2
                    selection = searchSelection(market, 30001);
                    if (null != selection) {
                        $('#score_0_2').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 30002);
                    if (null != selection) {
                        $('#score_1_2').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 30003);
                    if (null != selection) {
                        $('#score_2_2').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 30004);
                    if (null != selection) {
                        $('#score_3_2').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 30005);
                    if (null != selection) {
                        $('#score_4_2').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 30006);
                    if (null != selection) {
                        $('#score_5_2').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 30007);
                    if (null != selection) {
                        $('#score_6_2').text(selection.backOdds);
                    }
//n:3
                    selection = searchSelection(market, 40001);
                    if (null != selection) {
                        $('#score_0_3').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 40002);
                    if (null != selection) {
                        $('#score_1_3').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 40003);
                    if (null != selection) {
                        $('#score_2_3').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 40004);
                    if (null != selection) {
                        $('#score_3_3').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 40005);
                    if (null != selection) {
                        $('#score_4_3').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 40006);
                    if (null != selection) {
                        $('#score_5_3').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 40007);
                    if (null != selection) {
                        $('#score_6_3').text(selection.backOdds);
                    }
//n:4
                    selection = searchSelection(market, 50001);
                    if (null != selection) {
                        $('#score_0_4').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 50002);
                    if (null != selection) {
                        $('#score_1_4').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 50003);
                    if (null != selection) {
                        $('#score_2_4').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 50004);
                    if (null != selection) {
                        $('#score_3_4').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 50005);
                    if (null != selection) {
                        $('#score_4_4').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 50006);
                    if (null != selection) {
                        $('#score_5_4').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 50007);
                    if (null != selection) {
                        $('#score_6_4').text(selection.backOdds);
                    }
                    //n:5
                    selection = searchSelection(market, 60001);
                    if (null != selection) {
                        $('#score_0_5').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 60002);
                    if (null != selection) {
                        $('#score_1_5').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 60003);
                    if (null != selection) {
                        $('#score_2_5').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 60004);
                    if (null != selection) {
                        $('#score_3_5').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 60005);
                    if (null != selection) {
                        $('#score_4_5').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 60006);
                    if (null != selection) {
                        $('#score_5_5').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 60007);
                    if (null != selection) {
                        $('#score_6_5').text(selection.backOdds);
                    }
                    //n:6
                    selection = searchSelection(market, 70001);
                    if (null != selection) {
                        $('#score_0_6').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 70002);
                    if (null != selection) {
                        $('#score_1_6').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 70003);
                    if (null != selection) {
                        $('#score_2_6').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 70004);
                    if (null != selection) {
                        $('#score_3_6').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 70005);
                    if (null != selection) {
                        $('#score_4_6').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 70006);
                    if (null != selection) {
                        $('#score_5_6').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 70007);
                    if (null != selection) {
                        $('#score_6_6').text(selection.backOdds);
                    }
                    //其他
                    selection = searchSelection(market, 2);
                    if (null != selection) {
                        $('#score_2').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 3);
                    if (null != selection) {
                        $('#score_3').text(selection.backOdds);
                    }
                    selection = searchSelection(market, 4);
                    if (null != selection) {
                        $('#score_4').text(selection.backOdds);
                    }
                    $('#market_status_5').text(marketStatus(market));
                }

            }
        }
    })
}

/**
 * 设置禁用和启用
 * @param element
 */

function setDisabledAction(element) {
    // id="enabled_05" ruleType="140"
    var enabled = new Array();

    //首先获取当前禁用的值
    if (document.getElementById("enabled_01").getAttribute("status") == 0) {
        enabled.push(document.getElementById("enabled_01").getAttribute("ruleType"))
    }
    if (document.getElementById("enabled_02").getAttribute("status") == 0) {
        enabled.push(document.getElementById("enabled_02").getAttribute("ruleType"))
    }
    if (document.getElementById("enabled_03").getAttribute("status") == 0) {
        enabled.push(document.getElementById("enabled_03").getAttribute("ruleType"))
    }
    if (document.getElementById("enabled_04").getAttribute("status") == 0) {
        enabled.push(document.getElementById("enabled_04").getAttribute("ruleType"))
    }
    if (document.getElementById("enabled_05").getAttribute("status") == 0) {
        enabled.push(document.getElementById("enabled_05").getAttribute("ruleType"))
    }

    var ruleType = element.getAttribute("ruleType");
    var status = element.getAttribute("status");
    if (null != status) {
        if (1 == status) {
            //正则启用，需要禁用
            enabled.push(ruleType);
        } else if (0 == status) {
            //正在禁用，需要启用
            var index = enabled.indexOf(ruleType);
            if (index > -1) {
                enabled.splice(index, 1);
            }
        }
        var str = enabled.join(",");
        if (str.length == 0) {
            str = "0";
        }

        var timeStr = $('#card-title').text();

        var data = {
            "eventId": baseFn.getQueryString('eventId'),
            "ruleTypes": str,
            "homeName": $('#home_name_1').text(),
            "awayName": $('#away_name_1').text(),
            "eventTime": timeStr.substr(5, timeStr.length - 1),
        }

        $.ajax({
            url: baseUrl.base + 'customEvent/setEvent',
            type: 'get',
            data: data,
            success: function (data) {
                if (data.code == 0) {

                    if (status == 1) {
                        element.setAttribute("status", "0");
                        element.setAttribute("class", "btn btn-danger btn-sm");
                        element.innerHTML = "已禁用";
                        showNotification('bottom', 'right', '禁用成功');

                    } else {
                        element.setAttribute("status", "1");
                        element.setAttribute("class", "btn btn-info btn-sm");
                        element.innerHTML = "已启用";
                        showNotification('bottom', 'right', '已启成功');
                    }
                    findAllMarkets();
                }
            }
        });
    }
}

/**
 * 统计按钮
 */
function gotoCount(element) {
    var eventId = baseFn.getQueryString('eventId');
    var ruleType = element.getAttribute("ruleType");

    window.location.href = "market-count.do?eventId=" + eventId + '&ruleType=' + ruleType;
}

/**
 * 从列表中搜索某个玩法
 * @param markets
 * @param type
 * @returns {*}
 */
function searchMarket(result, type) {

    var key = 'market' + type;
    return result[key];
}

/**
 * 从market里筛选某个选项
 * @param markets
 * @param id
 * @returns {*}
 */
function searchSelection(market, id) {
    var selections = market.selections;
    for (var i = 0; i < selections.length; i++) {
        var selection = selections[i];
        if (selection.selectionId == id) {
            return selection;
        }
    }
    return null;
}

/**
 * 通过market获取状态字符串
 * @param market
 */
function marketStatus(market) {
    var str = '未开放';

    if (null != market) {
        var state = market.state;
        if (state == 0) {
            str = "暂无赔率";
        } else if (state == 1) {
            str = "正在开放";
        } else if (state == 2) {
            str = "已封盘";
        } else if (state == 3) {
            str = "已关闭";
        } else if (state == 4) {
            str = "已结算";
        } else if (state == 7) {
            str = "强制封盘";
        } else if (state == 9) {
            str = "强制封盘";
        }
    }
    return str;
}